package service;

import dao.ReviewDao;
import entity.Review;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ReviewService {

    private ReviewDao reviewDAO = new ReviewDao();

    // 提交评价
    public boolean submitReview(JSONArray reviews,int userId) {
        // 将 JSONArray 转换为 List<Review>
        List<Review> reviewsList = new ArrayList<>();
        for (int i = 0; i < reviews.length(); i++) {
            JSONObject reviewJson = reviews.getJSONObject(i);
            Review review = new Review();
            review.setProductId(reviewJson.getInt("productId"));
            review.setReviewText(reviewJson.getString("reviewText"));

            reviewsList.add(review);
        }
        return reviewDAO.insertReview(reviewsList,userId);
    }

    public List<Review> queryReviewsByProductId(int id) {
        return reviewDAO.queryReviewsByProductId(id);
    }

}
